<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas{
            /* border:1px solid black; */
        }
    </style>
</head>
<body>
    <canvas id="c1" width="500" height="200">该浏览器不支持该标签</canvas>
    <hr>
    <input type="button" value="开始" onclick="doStart()">
    <input type="button" value="结束" onclick="doStop()">
    <script>
        // 1.获取画板的DOM对象(打开画图工具)
        var c1=document.getElementById("c1");
        // 2.获取画笔工具(笔触)
        var ctx=c1.getContext("2d");
        var start=20;
        var step=1;
        var timer=null;
        function drawBall(){
            ctx.clearRect(0,0,500,200);
            ctx.beginPath();
            start=start+step;//start=20+1   start=21+1   ... start=199+1=200   start=200+(-1)=199  start=199+(-1)=198  start=1+(-1)=0
            ctx.fillStyle="blue";

            // arc(x,y,r,起始弧度,结束弧度)
            ctx.arc(250,start,20,0,2*Math.PI);
            ctx.fill();
            
            //判断临界点从而改变start的取值，通过step的值+1或者-1
            if(start==0||start==200){
                step=step*(-1);//step=-1  step=1
            }
        }

        function doStart(){
            timer=setInterval(drawBall,10);
        }
        
       
        
    </script>

</body>
</html>